package net.login.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.login.domain.User;
import net.login.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/27.
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
//    private String str;
    private Log log= LogFactory.getLog(this.getClass());

    private User model=new User();
    @Resource
    private UserService userService;


//    public String getStr() {
//        return str;
//    }
//
//    public void setStr(String str) {
//        this.str = str;
//    }

    public String loginUI(){
        return "loginUI";
    }

    public String login(){

        User user=userService.findByUsernamePassword(model.getUsername(),model.getPassword());
        if (user!=null){
//            str = "登录成功";
            this.addActionMessage("登录成功");
            ActionContext.getContext().getSession().put("user",user);
            List<User> userList=userService.findAll();
            ActionContext.getContext().put("userList",userList);
            return "success";
        }else {
//            str = "用户名或密码错误";
            this.addActionError("用户名或密码错误");
            this.addFieldError("username","用户名错误！");
            return "fail";
        }
    }

    @Override
    public User getModel() {
        return model;
    }
}
